package cn.liulin.java8.stream;

import cn.liulin.java8.pojo.Dish;

import java.util.Arrays;
import java.util.List;

/**
 * cn.liulin.java8.stream$
 * 查找和匹配
 * allMatch、anyMatch、noneMatch、findFirst和findAny
 * @author ll
 * @date 2021-10-14 11:25:44
 **/
public class StreamMatch {
    public static void main(String[] args) {
//        anyMatch();
//        allMatch();
//        noneMatch();
//        findAny();
        findFirst();
    }

    /**
     * findFirst查找第一个元素
     * @author ll
     * @date 2021-10-14 11:40:00
     * @return void
     **/
    private static void findFirst() {
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        integers.stream()
                .map(x -> x * x)
                .filter(x -> x % 3 == 0)
                .findFirst()
                .ifPresent(d -> System.out.println((int) Math.sqrt(d)));
    }

    /**
     * findAny 返回当前流中的任意元素。它可以与其他流操作结合使用
     * @author ll
     * @date 2021-10-14 11:36:37
     * @return void
     **/
    private static void findAny() {
        Dish dish1 = new Dish("hasd", true);
        Dish dish2 = new Dish("g", false);
        Dish dish3 = new Dish("asdf", false);
        Dish dish4 = new Dish("dfg", true);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4);
        menu.stream()
                .filter(Dish::isVegetarian)
                .findAny()
                .ifPresent(d -> System.out.println(d.getName()));
    }

    /**
     * noneMatch检查没有任何元素与给定的谓词匹配
     * @author ll
     * @date 2021-10-14 11:30:04
     * @return void
     **/
    private static void noneMatch() {
        Dish dish1 = new Dish("hasd", true);
        Dish dish2 = new Dish("g", false);
        Dish dish3 = new Dish("asdf", false);
        Dish dish4 = new Dish("dfg", true);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4);
        if (menu.stream().noneMatch(Dish::isVegetarian)) {
            System.out.println("所有元素不全部匹配");
        } else {
            System.out.println("所有元素不是全部不匹配");
        }
    }

    /**
     * allMatch检查谓词是否匹配所有元素
     * @author ll
     * @date 2021-10-14 11:28:55
     * @return void
     **/
    private static void allMatch() {
        Dish dish1 = new Dish("hasd", true);
        Dish dish2 = new Dish("g", false);
        Dish dish3 = new Dish("asdf", false);
        Dish dish4 = new Dish("dfg", true);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4);
        if (menu.stream().allMatch(Dish::isVegetarian)) {
            System.out.println("所有元素全部匹配");
        } else {
            System.out.println("所有元素不是全部匹配");
        }
    }

    /**
     * anyMatch检查谓词是否至少匹配一个元素
     * @author ll
     * @date 2021-10-14 11:26:32
     * @return void
     **/
    private static void anyMatch() {
        Dish dish1 = new Dish("hasd", true);
        Dish dish2 = new Dish("g", false);
        Dish dish3 = new Dish("asdf", false);
        Dish dish4 = new Dish("dfg", true);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4);
        if (menu.stream().anyMatch(Dish::isVegetarian)) {
            System.out.println("至少有一个匹配");
        }
    }
}
